<template>
  <el-dropdown :disabled="disabled">
    <el-button type="text" :disabled="disabled">
      <slot></slot>
      {{ title }}
      <el-icon class="el-icon--right"><arrow-down /></el-icon>
      <!-- <i class="el-icon-arrow-down el-icon--right"></i> -->
    </el-button>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item
          :disabled="disabled"
          v-for="(item, index) in titles"
          :key="index"
          @click="change(item)"
        >
          {{ item.label }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>
<script>
  import { computed } from '@vue/runtime-core'
  import { ArrowDown } from '@element-plus/icons-vue'
  export default {
    name: 'XmDropdown',
    components: { ArrowDown },
    props: {
      titles: {
        type: Array,
        default() {
          // {label: ,value:}
          return []
        },
      },
      // value
      value: [String, Number],
      disabled: Boolean,
    },
    emits: ['changeTitle'],
    setup(props, context) {
      const title = computed(() => {
        const item = props.titles.filter((item) => {
          return item.value + '' === props.value + ''
        })
        return Array.isArray(item) && item.length > 0 ? item[0].label : ''
      })
      const change = function (item) {
        if (props.disabled) return
        context.emit('changeTitle', item)
        context.emit('update', item.value)
      }
      return {
        title,
        change,
      }
    },
  }
</script>
<style lang="scss" scoped>
  .icon-color {
    color: var(--el-button-disabled-font-color);
  }
</style>
